package cn.hxzy.controller;


import cn.hxzy.pojo.Proinfo;
import cn.hxzy.service.ProinfoService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author songmengxin
 * @since 2021-07-31
 */
@CrossOrigin
@RestController
public class ProinfoController {

    @Autowired
    private ProinfoService proinfoService;

    @RequestMapping("/getProinfoById")
    public JSONObject selectById(@RequestParam String proId){
        System.out.println(proId);
        JSONObject jsonObject=new JSONObject();
        Proinfo byId = proinfoService.getProinfoById(proId);
        jsonObject.put("code",0);
        jsonObject.put("msg","获取数据成功");
        jsonObject.put("data",byId);
        return  jsonObject;
    }

    @RequestMapping("/setLikeNumById")
    public JSONObject setLikeNumById(@RequestParam String proId,@RequestParam Integer prolikeNum){
        Proinfo proinfo=new Proinfo();
        proinfo.setLikeNum(prolikeNum+1);
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("likeNum",prolikeNum);
        queryWrapper.eq("proId",proId);
        JSONObject jsonObject=new JSONObject();
        boolean update = proinfoService.update(proinfo, queryWrapper);
        jsonObject.put("code",0);
        jsonObject.put("msg","点赞成功");
        return  jsonObject;
    }
    @RequestMapping("/Proinfolist")
    public JSONObject Proinfolist(){
        JSONObject jsonObject=new JSONObject();
        List<Proinfo> list = proinfoService.list();
        jsonObject.put("code",0);
        jsonObject.put("msg","获取数据成功");
        jsonObject.put("count",list.size());
        jsonObject.put("data",list);
        return  jsonObject;
    }
}

